<?php
$model = new BDynamicFormModel();
$modelClass = get_class($model);
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$url = $control->getAjaxLinkUrl('saveMessage', NULL, array(
    'messageId' => $messageId,
    'parentEventId' => $eventId,
    'postModel' => $modelClass));
$updateId = $control->getAjaxContainerId();
$messageNameField = 'message_name_' . $messageId;
$imageField = 'image_media_image_' . $messageId;
$descriptionField = 'message_description_' . $messageId;

$model->addField($messageNameField);
$model->addField($imageField);
$model->addField($descriptionField);

if ($messageId > 0) {
    $message = new BWMessage($messageId);

    $model->$messageNameField = $message->name;
    $model->$descriptionField = $message->description;

    $imageMedia = $message->getMediaManager()->getMedia('image');
    if ($imageMedia)
        $model->$imageField = $message->getMediaManager()->getMedia('image')->content;
}
?>

<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
        function send<?php echo $messageId; ?>() {

        var formElement = document.getElementById("message-form");
        var formData = new FormData(formElement);

        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(data) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(data);
//                alert(data);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function() {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "message-form",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
?>

<legend>
    <h4><?php echo $control->getAjaxLink(array('name' => '活动列表', 'id' => 'showEventList')); ?>\
        <?php
        echo $control->getAjaxLink(array('name' => '活动内容', 'id' => 'showAddEvent'), array(
            'eventId' => $eventId));
        ?>\
        <?php
        echo $control->getAjaxLink(array('name' => '旅游美食列表', 'id' => 'showFoodList'), array(
            'eventId' => $eventId));
        ?>\
        美食内容
    </h4>
</legend>

<div class="tabbable tabs-right" style="margin-top: 15px;"> <!-- Only required for left/right tabs -->
    <div class="tab-content">       
        <div class="tab-pane active" id="tab1">                
            <table class="table table-bordered table-striped">
                <colgroup>
                    <col class="span1">
                    <col class="span7">
                </colgroup>
                <thead>
                    <tr>
                        <th>美食属性</th>
                        <th>详细内容</th>
                    </tr>
                </thead>
                <tbody>
                 <tr><td> <code>美食名称</code> </td>
                     <td>
                         <?php echo $form->textArea($model, $messageNameField, array('placeholder' => '美食名称...', 'required' => true)); ?>
                     </td>
                 </tr>
                  <tr><td><code>美食缩略图</code> </td>
                      <td>
                          <?php
                          echo $form->fileField($model, $imageField);
                          if ($messageId > 0) {
                              $imageUrl = Yii::app()->request->baseUrl . $model->$imageField;
                              echo "<img src=" . $imageUrl . " width=50 height=50 />";
                          }
                          ?>
                      </td>
                  </tr>
                  <tr>
                      <td> <code>美食描述</code>  </td>
                      <td>
                          <?php echo $form->textArea($model, $descriptionField, array('placeholder' => '美食描述...')); ?>
                      </td>
                  </tr>
             </tbody>
                </table>
        </div>
    </div>
</div>
<?php $this->endWidget(); ?>
<button class="btn" onclick="javascript:send<?php echo $messageId; ?>()">保存</button>